package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.common.constant.RedisConstant;
import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.common.utils.JwtUtil;
import com.atguigu.lease.common.utils.VerifyCodeUtil;
import com.atguigu.lease.model.entity.UserInfo;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.app.mapper.UserInfoMapper;
import com.atguigu.lease.web.app.service.LoginService;
import com.atguigu.lease.web.app.service.SmsService;
import com.atguigu.lease.web.app.vo.user.LoginVo;
import com.atguigu.lease.web.app.vo.user.UserInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {
@Autowired
private SmsService smsService;
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private UserInfoMapper userInfoMapper;
    @Override
    public void getCode(String phone) {
    String code = VerifyCodeUtil.codeBuilder(6);
    String key = RedisConstant.ADMIN_LOGIN_PREFIX + phone;
        Boolean hasKey = redisTemplate.hasKey(key);
        if(hasKey){
            Long expire = redisTemplate.getExpire(key);
            if(RedisConstant.ADMIN_LOGIN_CAPTCHA_TTL_SEC-expire<RedisConstant.APP_LOGIN_CODE_RESEND_TIME_SEC){
                throw new LeaseException(ResultCodeEnum.APP_SEND_SMS_TOO_OFTEN);
            }
        }//查看验证码两次发送时间是否少于60秒
        smsService.getSmsCode(phone,code);//前面校验没问题，则发送验证码
        redisTemplate.opsForValue().set(key,code,RedisConstant.ADMIN_LOGIN_CAPTCHA_TTL_SEC, TimeUnit.SECONDS);

    }

    @Override
    public String login(LoginVo loginVo) {
        if(loginVo.getPhone()==null)
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_PHONE_EMPTY);
        if(loginVo.getCode()==null)
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_CODE_EMPTY);

        String key = RedisConstant.ADMIN_LOGIN_PREFIX + loginVo.getPhone();
        String code= (String) redisTemplate.opsForValue().get(key);
        if(code == null){
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_CODE_EXPIRED);
        }
        if(!code.equals(loginVo.getCode())){
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_CODE_ERROR);
        }
        LambdaQueryWrapper<UserInfo> userInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userInfoLambdaQueryWrapper.eq(UserInfo::getPhone,loginVo.getPhone());
        UserInfo userInfo = userInfoMapper.selectOne(userInfoLambdaQueryWrapper);
        if(userInfo==null){
            userInfo=new UserInfo();
            userInfo.setPhone(loginVo.getPhone());
            userInfo.setNickname("用户"+loginVo.getPhone());
            userInfo.setStatus(BaseStatus.ENABLE);
            userInfoMapper.insert(userInfo);
        }else{
            if(userInfo.getStatus()==BaseStatus.DISABLE){
                throw new LeaseException(ResultCodeEnum.APP_ACCOUNT_DISABLED_ERROR);
            }
        }

        return JwtUtil.createToken(userInfo.getId(),loginVo.getPhone());
    }

    @Override
    public UserInfoVo info(Long userID) {

        UserInfo userInfo = userInfoMapper.selectById(userID);
        //虽然用户信息很多，但是这里只需要展示两个，昵称和头像，所以不需要把userInfo所有信息复制到userInfoVo里
        UserInfoVo userInfoVo = new UserInfoVo(userInfo.getNickname(),userInfo.getAvatarUrl());
        return userInfoVo;

    }
}
